/*
 * soc_script_port.c
 *
 *  Created on: 2019年4月7日
 *      Author: 0yfj0
 */

#include <stdlib.h>
#include <string.h>
#include "soc_script_port.h"

#ifndef SOC_SCRIPT_PORT_DEBUG
#define SOC_SCRIPT_PORT_DEBUG	( 1 )
#endif

#if SOC_SCRIPT_PORT_DEBUG
#include <stdio.h>
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define SOC_A9G_PORT			( 0 )
#if SOC_A9G_PORT
#include <drv_hal.h>
#include <api_os.h>
#endif

void *_soc_script_malloc(uint16_t sz)
{
	return malloc(sz);
}

void _soc_script_free(void *buf)
{
	free(buf);
}

void _soc_script_wait(uint32_t ms)
{
	/* 延时 */
#if SOC_SCRIPT_PORT_DEBUG
	DEBUG("%s: wait %d\n", __FUNCTION__, ms);
#endif

#if SOC_A9G_PORT
	OS_Sleep(ms);
#endif
}

int _soc_script_hhmm(void)
{
	/* 返回当前时间00:00-0, 23:55-2355 */
#if SOC_SCRIPT_PORT_DEBUG
	DEBUG("%s: hhss %d\n", __FUNCTION__, 2230);
#endif

#if SOC_A9G_PORT
	return sys.hhmm;
#endif
//	return 0;
}

int _soc_script_gpio_get(int n, int *val)
{
#if SOC_SCRIPT_PORT_DEBUG
	*val = 0;
	DEBUG("%s: GPIO%d:%d\n", __FUNCTION__, n, (int )*val);
#endif

#if SOC_A9G_PORT
	if (val == NULL)
		return -1;
	if (drv_hal_gpio_get(n, val) != 0)
		return -1;
#endif
	return 0;
}

int _soc_script_analog_get(int n, int *val)
{
#if SOC_SCRIPT_PORT_DEBUG
	*val = 0;
	DEBUG("%s: ANALOG%d:%d\n", __FUNCTION__, n, (int )*val);
#endif

#if SOC_A9G_PORT
	if (val == NULL)
		return -1;
	if (drv_hal_analog_get(n, val) != 0)
		return -1;
#endif
	return 0;
}

int _soc_script_output_get(int n, int *val)
{
#if SOC_SCRIPT_PORT_DEBUG
	*val = 0;
	DEBUG("%s: OUTPUT%d:%d\n", __FUNCTION__, n, (int )*val);
#endif

#if SOC_A9G_PORT
	if (drv_hal_output_get(n, val) != 0)
		return -1;
#endif
	return 0;
}

int _soc_script_input_get(int n, int *val)
{
#if SOC_SCRIPT_PORT_DEBUG
	*val = 0;
	DEBUG("%s: INPUT%d:%d\n", __FUNCTION__, n, (int )*val);
#endif

#if SOC_A9G_PORT
	if (drv_hal_input_get(n, val) != 0)
		return -1;
#endif
	return 0;
}

int _soc_script_gpio_set(int n, int val)
{
#if SOC_SCRIPT_PORT_DEBUG
	DEBUG("%s: GPIO%d=%d\n", __FUNCTION__, n, val);
#endif

#if SOC_A9G_PORT
	if (val == NULL)
		return -1;

	if (drv_hal_gpio_set(n, val) != 0)
		return -1;
#endif
	return 0;
}

int _soc_script_output_set(int n, int val)
{
	/* TODO: */
#if SOC_SCRIPT_PORT_DEBUG
	DEBUG("%s: OUTPUT%d=%d\n", __FUNCTION__, n, val);
#endif

#if SOC_A9G_PORT
	if (drv_hal_output_set(n, val) != 0)
		return -1;
#endif
	return 0;
}
